package com.example.doer.reading;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;


/**
 * Created by 严旭江 on 2017/7/11.
 */

public class MyTopicAdapter extends BaseAdapter {

    List<Topic> topicList;
    Context context;
    int resID;

    public MyTopicAdapter(Context theContext, List<Topic> theList, int theResID) {
        this.context = theContext;
        this.topicList = theList;
        this.resID = theResID;
    }

    @Override
    public int getCount() {
        return topicList.size();
    }

    @Override
    public Topic getItem(int position) {
        return topicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //在TopicFragment中显示每个话题view
        View view = LayoutInflater.from(context).inflate(resID, null);

        //获取在位置position的Topic对象的引用
        final Topic topic = getItem(position);


        //获取每个View的各个控件的引用
        ImageView topicImageView = (ImageView)view.findViewById(R.id.topicImageView);
        TextView catagory = (TextView) view.findViewById(R.id.topicCatagory);
        TextView topicName = (TextView) view.findViewById(R.id.topicName);
        TextView topicContent = (TextView) view.findViewById(R.id.topicContent);
        TextView likeCount = (TextView) view.findViewById(R.id.topicAgreeNumber);
        TextView dislikeCount = (TextView) view.findViewById(R.id.topicDislikeNumber);
        TextView commentCount = (TextView) view.findViewById(R.id.topicCommentNumber);

        topicImageView.setImageResource(R.drawable.user_icon_test);
        catagory.setText(topic.getTopicCatagory());
        topicName.setText(topic.getTopicName());
        topicContent.setText(topic.getTopicContent());

        likeCount.setText(String.valueOf(topic.getFavoCount()+ "赞同"));
        dislikeCount.setText(String.valueOf(topic.getDislikeCount()+" 不赞同"));
        commentCount.setText(String.valueOf(topic.getReplyCount()+ " 回复数目"));

        //点击话题内容进行页面转换
        ((TextView)view.findViewById(R.id.topicContent)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //使用topic
                Intent intent = new Intent(v.getContext(),TopicActivity.class);
                intent.putExtra("topicReplyObj",topic);
                v.getContext().startActivity(intent);
                //((Activity)v.getContext()).finish();
            }
        });


        //点击话题名进行页面转换
        ((TextView)view.findViewById(R.id.topicName)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //使用topic
                Intent intent = new Intent(v.getContext(),TopicWithReplyActivity.class);
                intent.putExtra("topicObjWithReply",topic);
                v.getContext().startActivity(intent);
                //((Activity)v.getContext()).finish();
            }
        });

        return view;
    }
}
//    private LayoutInflater mInflater;//LayoutInflater是用来找layout下xml布局文件，并且实例化
//    private List<Topic>topicList;
//
//    public MyTopicAdapter(Context context,List<Topic> list) {
//        mInflater=LayoutInflater.from(context); //得到初始化上下文
//        topicList = list;
//    }
//    @Override
//    public int getCount(){
//        return topicList.size();
//    }
//    @Override
//    public Object getItem(int position) {
//        return topicList.get(position);
//    }
//    public long getItemId(int position){
//        return position;
//    }
//    @Override
//    public View getView(int position, View convertView, ViewGroup parent) {
//        if(convertView==null) {
//            convertView = mInflater.inflate(R.layout.topic_listview, null);
//            //获取在位置position的Topic对象的引用
//            Topic topic = (Topic) getItem(position);
//
//            //获取每个View的各个控件的引用
//            TextView catagory = (TextView) convertView.findViewById(R.id.topicCatagory);
//            TextView topicName = (TextView) convertView.findViewById(R.id.topicName);
//            TextView topicContent = (TextView) convertView.findViewById(R.id.topicContent);
//            TextView likeCount = (TextView) convertView.findViewById(R.id.topicAgreeNumber);
//            TextView dislikeCount = (TextView) convertView.findViewById(R.id.topicDislikeNumber);
//            TextView commentCount = (TextView) convertView.findViewById(R.id.topicCommentNumber);
//            Button btnDelete = (Button) convertView.findViewById(R.id.deleteTopicBtn);
//
//            catagory.setText(topic.getTopicCatagory());
//            topicName.setText(topic.getTopicName());
//            topicContent.setText(topic.getTopicContent());
//            likeCount.setText(String.valueOf(topic.getFavoCount()));
//            dislikeCount.setText(String .valueOf(topic.getDislikeCount()));
//            commentCount.setText(String.valueOf(topic.getReplyCount()));
//            btnDelete.setText("X");
//
//            //为真个View添加监听
//            //addListener(convertView);
//        }
//        return convertView;
//    }//end method

//    private void addListener(View view){
//        ((Button)view.findViewById(R.id.deleteTopicBtn)).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(get,TopicActivity.class);
//                startActivity(intent);
//                getActivity().finish();
//            }
//        });
//    }

